<?php
class classOrderData{
    public function iInsert($Delivery_ID, $Payment_ID, $Additional_Text){
        $_BasketService = new classBasketService();
        
        $testContent = $_BasketService->iCount();
        $step = $_BasketService->aGetFirst();

        //////////////////////////////////
        if(_DBG_MODE_=='YES') file_put_contents('plugins/order/results/1.txt', '$testContent (pocet poloziek v kosiku)-> '.$testContent.' $step (nastavenie sa na prvu polozku v kosiku)-> '.$step);
        //////////////////////////////////
        
        if($step!==false){            
            $OH_ID = $this->iInsertOrderHead($Delivery_ID, $Payment_ID, $Additional_Text);
            
            if($OH_ID!='' and is_numeric($OH_ID) and $OH_ID>0){
                $this->iInsertOrderProduct($OH_ID, $step['ProductID'], $step['ProductCount']);
                while($step = $_BasketService->aGetNext()){
                    $this->iInsertOrderProduct($OH_ID, $step['ProductID'], $step['ProductCount']);
                }
            }
            else return 0;
            
        }

        return $OH_ID;
    }
    
    private function iInsertOrderHead($Delivery_ID, $Payment_ID, $Additional_Text){        
        $_UserService = new classUsersService();
        
        if ($_UserService->bIsLogged() or $_UserService->bIsSetUnloggedUser()) {
            if ($_UserService->bIsLogged()) $User = $_UserService->getUserInfo();
            if ($_UserService->bIsSetUnloggedUser()) $User = $_UserService->getUnloggedUserInfo();

            //////////////////////////////////
            if(_DBG_MODE_=='YES'){
                file_put_contents('plugins/order/results/15.txt', '$User-> '.$User['ID'].',
                    "'.$User['U_Name'].'",
                    "'.$User['U_Surname'].'",
                    "'.$User['U_City'].'",
                    "'.$User['U_Street'].'",
                    "'.$User['U_PostID'].'",
                    "'.$User['U_Phone'].'",
                    "'.$User['U_Mobile'].'",
                    "'.$User['U_Email'].'"');
            }
            //////////////////////////////////
            
            $Query_Order_Heads  = '
                  INSERT INTO order_heads (
                    NO, OH_UserID, OH_Name, OH_Surname, OH_City, OH_Street, OH_PostID,
                    OH_Phone, OH_Mobile, OH_Email, OH_BirthDate, Order_Create, 
                    OH_Delivery, OH_Payment, OH_Organisation, OH_ICO, OH_DIC, OH_Text
                  )
                  VALUES(
                    '.$this->getNextOrderNO().',
                    '.$User['ID'].',
                    "'.$User['U_Name'].'",
                    "'.$User['U_Surname'].'",
                    "'.$User['U_City'].'",
                    "'.$User['U_Street'].'",
                    "'.$User['U_PostID'].'",
                    "'.$User['U_Phone'].'",
                    "'.$User['U_Mobile'].'",
                    "'.$User['U_Email'].'",
                    "'.$User['U_BirthDate'].'",
                    "'.date(_DB_SERVER_LOCAL_DATE_FORMAT_).'",
                    '.$Delivery_ID.',
                    '.$Payment_ID.',
                    "'.$User['U_Organisation'].'",
                    "'.$User['U_ICO'].'",
                    "'.$User['U_DIC'].'",
                    "'.$Additional_Text.'"
                  )
            ';    //return $Query_Order_Heads;
            //save data and retrieve new ID of saved order
            @$dbresult = mysql_query($Query_Order_Heads);
            $OH_ID = mysql_insert_id();

            //////////////////////////////////
            if(_DBG_MODE_=='YES') file_put_contents('plugins/order/results/2.txt', '$Query_Order_Heads: '.$Query_Order_Heads.'..... $OH_ID-> '.$OH_ID.' ........... @$dbresult->'.@$dbresult);
            //////////////////////////////////

            if ($OH_ID==0) return 0;
            return $OH_ID;
        }
        else return 0;
    }
    
    private function iInsertOrderProduct($iOrderID, $iProductID, $iProductCount){
        $_UserService = new classUsersService();
        //return 'hello world';
        if ($_UserService->bIsLogged() or $_UserService->bIsSetUnloggedUser()){
            $_ProductService = new classProductService();
            $Product = $_ProductService->getProductInfo($iProductID);
            
            $Query_Order_Product  = '
                INSERT INTO order_products (
                    OH_ID, OP_Code, OP_Name, OP_Price, OP_DPH, 
                    OP_Count, OP_UnitType, OP_ID
                )
                VALUES(
                    '.$iOrderID.',
                    "'.$Product['Product_Code'].'",
                    "'.$Product['Product_Name'].'",
                    '.$Product['Product_Price'].',
                    '.$Product['Product_DPH'].',
                    '.$iProductCount.',
                    "'.$Product['Product_UnitType'].'",
                    '.$iProductID.'
                )
            ';
            //return $Query_Order_Product;
            @$dbresult = mysql_query($Query_Order_Product);
            $OP_ID = mysql_insert_id();

            //////////////////////////////////
            if(_DBG_MODE_=='YES') file_put_contents('plugins/order/results/3.txt', '$Query_Order_Product: '.$Query_Order_Product.'..... $OP_ID-> '.$OP_ID.' ........... @$dbresult->'.@$dbresult);
            //////////////////////////////////

            if ($OP_ID==0) return 0;
            return $OP_ID;
        }
    }

    private function getNextOrderNO(){
        $sql_result = mysql_query('SELECT MAX(NO) as maxNO FROM order_heads WHERE YEAR(Order_Create) = YEAR(NOW() )');
        $sql_object = mysql_fetch_object($sql_result);

        //////////////////////////////////
        if(_DBG_MODE_=='YES') file_put_contents('plugins/order/results/12.txt', '$sql_object->maxNO: '.$sql_object->maxNO);
        //////////////////////////////////

        if(trim($sql_object->maxNO) == '') return 1;
        elseif(is_numeric($sql_object->maxNO) and $sql_object->maxNO>0) return $sql_object->maxNO+1;

        return 1;
    }
}
?>
